package com.gx.smart.building.ui.report.viewmodel

import android.app.Application
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.casic.gx.grpc.common.ComQuery
import com.gx.smart.building.PlanTaskApiService
import com.gx.smart.building.SysDictApiService
import com.gx.smart.building.WorkOrderApiService
import com.gx.smart.building.ui.common.viewmodel.CommonViewModel
import com.gx.smart.common.util.AppConfig
import com.gx.smart.lib.http.api.RetrofitManager
import com.gx.smart.lib.http.api.asynctask.CallBack
import com.gx.smart.lib.http.api.config.ApiConfig
import com.gx.smart.lib.http.api.response.WorkOrder
import com.huishi.gx.ib.grpc.lib.ib.app.plantask.v1.UploadWorkOrderByTaskAssetResp
import com.huishi.gx.ib.grpc.lib.ib.app.workorder.v1.InsertWorkOrderResp
import com.huishi.gx.ib.grpc.lib.ib.app.workorder.v1.QueryWorkOrderListResp
import com.huishi.gx.ib.grpc.lib.ib.app.workorder.v1.WorkOrderResp
import com.huishi.gx.ib.grpc.lib.ib.app.workorder.v1.dto.WorkOrderDto
import com.huishi.gx.ib.grpc.lib.ib.app.workorder.v1.dto.WorkOrderFileDto
import com.huishi.gx.ib.grpc.lib.sysdict.sysdictapi.SysDictQueryByParentResp
import com.huishi.gx.ib.grpc.lib.sysdict.sysdictapi.SysDictQueryItemDto
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

/**
 * @author: xiaosy
 * 时间: 2022/4/29
 * @Description: java类作用描述
 * @Version: 1.0
 */
class FaultReportViewModel(app: Application) : CommonViewModel(app) {
    val orderList: MutableLiveData<List<WorkOrderDto>> = MutableLiveData()
    private val typeList: MutableLiveData<List<SysDictQueryItemDto>> = MutableLiveData()
    private val subTypeList: MutableLiveData<List<SysDictQueryItemDto>> = MutableLiveData()
    private val addSuccess: MutableLiveData<WorkOrderDto> = MutableLiveData()
    private val evaluateSuccess: MutableLiveData<Boolean> = MutableLiveData()
    private val cancelSuccess: MutableLiveData<Boolean> = MutableLiveData()
    private val detail: MutableLiveData<WorkOrder> = MutableLiveData()
    private val reportSuccess: MutableLiveData<Boolean> = MutableLiveData()

    //通过父级的字典编码或者是父级的字典ID查询子集字典列表（只查一级）
    fun queryDict(
        parentTag: String
    ): LiveData<List<SysDictQueryItemDto>> {
        SysDictApiService.getInstance()
            .queryDictByParentId(
                parentTag,
                object : CallBack<SysDictQueryByParentResp>() {
                    override fun callBack(result: SysDictQueryByParentResp?) {
                        if (result == null) {
                            ToastUtils.showLong("服务器超时")
                            return
                        }
                        val comResp = result.comResp
                        if (comResp.code == 100) {
                            typeList.value = result.contentList
                        } else {
                            handleServiceException(comResp)
                        }
                    }
                })
        return typeList
    }

    //通过父级的字典编码或者是父级的字典ID查询子集字典列表（只查一级）
    fun queryDictByParentId(
        parentTag: String
    ): LiveData<List<SysDictQueryItemDto>> {
        SysDictApiService.getInstance()
            .queryDictByParentId(
                parentTag,
                object : CallBack<SysDictQueryByParentResp>() {
                    override fun callBack(result: SysDictQueryByParentResp?) {
                        if (result == null) {
                            ToastUtils.showLong("服务器超时")
                            return
                        }
                        val comResp = result.comResp
                        if (comResp.code == 100) {
                            subTypeList.value = result.contentList
                        } else {
                            handleServiceException(comResp)
                        }
                    }
                })
        return subTypeList
    }

    /**
     *查询工单列表
     */
    fun queryWorkOrderList(currentPage: Int) {
        val comQuery = ComQuery.newBuilder()
            .setPage(currentPage)
            .setPageSize(10)
            .build()
        val userId = SPUtils.getInstance().getString(AppConfig.USER_ID)
        WorkOrderApiService.getInstance()
            .queryWorkOrderList2(comQuery, userId, object : CallBack<QueryWorkOrderListResp?>() {
                override fun callBack(result: QueryWorkOrderListResp?) {
                    if (result == null) {
                        ToastUtils.showLong("服务器超时")
                        return
                    }
                    val comResp = result.comResp
                    if (comResp.code == 100) {
                        orderList.value = result.workOrderDtoList
                    } else {
                        handleServiceException(comResp)
                    }
                }
            })
    }

    //新增工单
    fun insertWorkOrder(
        orderTypeId: String,
        orderTypeCode: String,
        orderTypeName: String,
        orderSubTypeId: String,
        orderSubTypeCode: String,
        orderSubTypeName: String,
        areaName: String,
        description: String,
        releaseId: String,
        releaseUserName: String,
        releasePhone: String,
        files: List<WorkOrderFileDto>,
        assetId: String,
        assetName: String,
    ): LiveData<WorkOrderDto> {
        WorkOrderApiService.getInstance()
            .insertWorkOrder(
                orderTypeId,
                orderTypeCode,
                orderTypeName,
                orderSubTypeId,
                orderSubTypeCode,
                orderSubTypeName,
                areaName,
                description,
                releaseId,
                releaseUserName,
                releasePhone,
                files,
                assetId,
                assetName,
                object : CallBack<InsertWorkOrderResp?>() {
                    override fun callBack(result: InsertWorkOrderResp?) {
                        if (result == null) {
                            ToastUtils.showLong("服务器超时")
                            return
                        }
                        val comResp = result.comResp
                        if (comResp.code == 100) {
                            addSuccess.value = result.workOrderDto
                        } else {
                            handleServiceException(comResp)
                        }
                    }
                })
        return addSuccess
    }

    //工单评价
    // //工单ID
    //  string work_order_id = 3;
    fun evaluateTask(
        workOrderId: String,
        starNumber: Int,
        starNumberQuality: Int,
        opinion: String
    ): LiveData<Boolean> {
        WorkOrderApiService.getInstance()
            .evaluateTask(
                workOrderId,
                starNumber,
                starNumberQuality,
                opinion,
                object : CallBack<WorkOrderResp>() {
                    override fun callBack(result: WorkOrderResp?) {
                        if (result == null) {
                            ToastUtils.showLong("服务器超时")
                            return
                        }
                        val comResp = result.comResp
                        if (comResp.code == 100) {
                            evaluateSuccess.value = true
                        } else {
                            handleServiceException(comResp)
                        }
                    }
                })
        return evaluateSuccess
    }

    //工单撤销
    fun cancelTask(workOrderId: String): LiveData<Boolean> {
        com.gx.smart.building.admin.WorkOrderApiService.getInstance()
            .cancelTask(
                workOrderId,
                object : CallBack<com.huishi.gx.ib.grpc.lib.ib.admin.workorder.v1.WorkOrderResp>() {
                    override fun callBack(result: com.huishi.gx.ib.grpc.lib.ib.admin.workorder.v1.WorkOrderResp?) {
                        if (result == null) {
                            ToastUtils.showLong("服务器超时")
                            return
                        }
                        val comResp = result.comResp
                        if (comResp.code == 100) {
                            cancelSuccess.value = true
                        } else {
                            handleServiceException(comResp)
                        }
                    }
                })
        return cancelSuccess
    }

    /**
     * 查看工单详情
     */
    fun queryWorkOrderDetail(id: String): LiveData<WorkOrder> {
        RetrofitManager.getInstance().workOrderApiService.queryAppWorkOrderDetail().enqueue(object :
            Callback<com.gx.smart.lib.http.api.response.QueryWorkOrderDetailResp> {
            override fun onResponse(
                call: Call<com.gx.smart.lib.http.api.response.QueryWorkOrderDetailResp>,
                response: Response<com.gx.smart.lib.http.api.response.QueryWorkOrderDetailResp>
            ) {
                val result = response.body()
                if (result == null) {
                    ToastUtils.showLong("服务器超时")
                    return
                }
                val comResp = result.comResp
                if (comResp.code == 100) {
                    detail.value = result.workOrderDto
                } else {
                    ToastUtils.showLong(ApiConfig.SERVER_ERROR_MESSAGE)
                }
            }

            override fun onFailure(
                call: Call<com.gx.smart.lib.http.api.response.QueryWorkOrderDetailResp>,
                t: Throwable
            ) {
                ToastUtils.showLong(ApiConfig.SERVER_ERROR_MESSAGE)
            }

        })
        return detail
    }

    //故障上报，此接口在故障工单新增后，提交新增工单id和编号用于标记此任务已上报工单
    fun uploadWorkOrderByTaskAsset(
        orderId: String,
        orderCode: String,
        planTaskAssetId: String
    ): LiveData<Boolean> {
        PlanTaskApiService.getInstance().uploadWorkOrderByTaskAsset(
            orderId,
            orderCode,
            planTaskAssetId,
            object : CallBack<UploadWorkOrderByTaskAssetResp>() {
                override fun callBack(result: UploadWorkOrderByTaskAssetResp?) {
                    if (result == null) {
                        ToastUtils.showLong("服务器超时")
                        return
                    }
                    val comResp = result.comResp
                    if (comResp.code == 100) {
                        reportSuccess.value = true
                    } else {
                        handleServiceException(comResp)
                    }
                }
            })
        return reportSuccess
    }

}